﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Final
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“schoolsDataSet.Students”中。您可以根据需要移动或删除它。
            // this.studentsTableAdapter.Fill(this.schoolsDataSet.Students);

            var hhh = "select * from Students";

            var dt = Dbhelper.GetDataTable(hhh);

            dataGridView1.DataSource = dt;

            //限制dataGridView1的某些行为

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//整行单元格

            dataGridView1.ReadOnly = true;//禁止编辑

            dataGridView1.AllowUserToAddRows = false;
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            var name = textBox1.Text;

            var sql = string.Format("select * from Students where Name like '%{0}%'", name);

            var dt = Dbhelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Editform form = new Editform();
            var res = form.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var Sql = "select * from Students";

                var dt = Dbhelper.GetDataTable(Sql);

                dataGridView1.DataSource = dt;


            }
            else
            {
                MessageBox.Show("您取消了添加！","警告");

            }




        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            var id = (int)dataGridView1.SelectedRows[0].Cells["id"].Value;
            var name = (string)dataGridView1.SelectedRows[0].Cells["name"].Value;
            var age = (int)dataGridView1.SelectedRows[0].Cells["age"].Value;
            var score = (int)dataGridView1.SelectedRows[0].Cells["score"].Value;

            Editform form = new Editform(id, name, age, score);
            var res = form.ShowDialog();
            if (res == DialogResult.Yes)
            {

                var stuSql = "select * from Students";

                var dt = Dbhelper.GetDataTable(stuSql);

                dataGridView1.DataSource = dt;


            }
            else
            {
                MessageBox.Show("您取消了更新！","警告");



            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            var id = (int)dataGridView1.SelectedRows[0].Cells["id"].Value;

            var sql = string.Format("delete from Students where id={0}", id);

            this.DialogResult = DialogResult.Yes;

            if (MessageBox.Show("是否删除？", "提示", MessageBoxButtons.YesNo) == this.DialogResult)
            {
                var res = Dbhelper.AddOrUpdateOrDelete(sql);
                if (res > 0)
                {
                    MessageBox.Show("删除成功！", "提示");

                    var qqq = "select * from Students";

                    var dt = Dbhelper.GetDataTable(qqq);

                    dataGridView1.DataSource = dt;
                }

            }
            else
            {
                MessageBox.Show("删除失败！", "警告");

                var qqq = "select * from Students";

                var dt = Dbhelper.GetDataTable(qqq);

                dataGridView1.DataSource = dt;

            }
        }
    }
}
